import {PlotTypes} from '../enum/PlotTypes';

const rgb2Rgba = (color: string, opacity: number): string => {
    const rgbaAttr: Array<string> = color.match(/[\d.]+/g) as Array<string>;
    if (rgbaAttr.length >= 3) {
        const r: string = rgbaAttr[0];
        const g: string = rgbaAttr[1];
        const b: string = rgbaAttr[2];
        return `rgba(${r}, ${g}, ${b}, ${opacity})`;
    } else {
        return 'rgba(255, 255, 255, 1)';
    }
};

const getColor = (color: string): string => {
    const rgbaAttr: Array<string> = color.match(/[\d.]+/g) as Array<string>;
    if (rgbaAttr.length >= 3) {
        const r: string = rgbaAttr[0];
        const g: string = rgbaAttr[1];
        const b: string = rgbaAttr[2];
        return `rgb(${r}, ${g}, ${b})`;
    }
    return `rgb(255, 255, 255)`;
};

const getOpacity = (color: string): number => {
    let alp: number = 1;
    let rgbaReg = /rgba\([\d ]+(?:,([\d. ]+)){3}\)/;
    if (rgbaReg.test(color)) {
        alp = Number(color.replace(rgbaReg, '$1'));
    }
    return alp;
};

const formatPlotStyle = (type: PlotTypes, params: any): any => {
    switch (type) {
        case PlotTypes.TEXT_AREA:
            return {
                fontFamily: 'Microsoft Yahei UI',
                fontSize: 18,
                borderColor: 'rgb(255, 34, 125)',
                borderWeight: 2,
                borderOpacity: 0.5,
                borderStyle: 'solid',
                backgroundColor: 'rgb(255, 0, 255)',
                backgroundOpacity: 0.7
            };
        case PlotTypes.POINT:
            return {
                anchor: params.getImage().getAnchor(),
                url: params.getImage().getSrc()
            };
        case PlotTypes.CIRCLE:
            return {
                strokeColor: getColor(params.getStroke().getColor()),
                strokeWeight: Number(params.getStroke().getWidth()),
                strokeOpacity: getOpacity(params.getStroke().getColor()),
                strokeStyle: 'solid',
                fillColor: getColor(params.getFill().getColor()),
                fillOpacity: getOpacity(params.getFill().getColor())
            };
        case PlotTypes.FREE_HAND_LINE:
        case PlotTypes.SEGMENT_LINE:
        case PlotTypes.POLYLINE:
            return {
                color: getColor(params.getStroke().getColor()),
                weight: Number(params.getStroke().getWidth()),
                opacity: getOpacity(params.getStroke().getColor()),
                style: 'solid'
            };
        case PlotTypes.RECTANGLE:
        case PlotTypes.POLYGON:
        case PlotTypes.RECT_FLAG:
        case PlotTypes.TRIANGLE_FLAG:
        case PlotTypes.DOUBLE_ARROW:
        case PlotTypes.FINE_ARROW:
        case PlotTypes.STRAIGHT_ARROW:
        case PlotTypes.SQUAD_COMBAT:
            return {
                strokeColor: getColor(params.getStroke().getColor()),
                strokeWeight: Number(params.getStroke().getWidth()),
                strokeOpacity: getOpacity(params.getStroke().getColor()),
                fillColor: getColor(params.getFill().getColor()),
                fillOpacity: getOpacity(params.getFill().getColor())
            };
    }
};

const formatPlotData = (type: PlotTypes, feature: any): any => {
    const style: any = formatPlotStyle(type, feature.getStyle());
    switch (type) {
        case PlotTypes.POINT:
            return {
                point: feature.getGeometry().getPoints()[0],
                style: {...style, size: feature.getProperties().size}
            };
        case PlotTypes.FREE_HAND_LINE:
        case PlotTypes.SEGMENT_LINE:
        case PlotTypes.POLYLINE:
            return {
                path: feature.getGeometry().getPoints(),
                style
            };
        case PlotTypes.CIRCLE:
            return {
                radius: feature.getGeometry().getRadius(),
                path: feature.getGeometry().getPoints(),
                style
            };
        case PlotTypes.RECTANGLE:
            return {
                path: feature.getGeometry().getPoints(),
                style
            };
        case PlotTypes.POLYGON:
            return {
                path: feature.getGeometry().getPoints(),
                style
            };
        case PlotTypes.TEXT_AREA:
            return {
                center: feature.getPosition(),
                style
            };
        case PlotTypes.RECT_FLAG:
        case PlotTypes.TRIANGLE_FLAG:
        case PlotTypes.DOUBLE_ARROW:
        case PlotTypes.FINE_ARROW:
        case PlotTypes.STRAIGHT_ARROW:
        case PlotTypes.SQUAD_COMBAT:
            return {
                path: feature.getGeometry().getPoints(),
                style
            };
    }
};

export {rgb2Rgba, formatPlotData};